package com.nolon.flange.tools

import android.annotation.SuppressLint
import android.app.ActivityManager
import android.content.Context
import java.text.DecimalFormat
import java.text.SimpleDateFormat
import java.time.Instant
import java.time.ZoneId
import java.util.*
import java.util.regex.Matcher
import java.util.regex.Pattern


var chars = arrayOf(
    "a", "b", "c", "d", "e", "f",
    "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
    "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
    "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
    "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
    "W", "X", "Y", "Z"
)

fun createID(): String {
    val shortBuffer = StringBuffer()
    val uuid: String = UUID.randomUUID().toString().replace("-", "")
    for (i in 0..7) {
        val str = uuid.substring(i * 4, i * 4 + 4)
        val x = str.toInt(16)
        shortBuffer.append(chars[x % 0x3E])
    }
    return shortBuffer.toString()
}

@SuppressLint("NewApi")
fun Long.toDate(): String {
    return Instant.ofEpochMilli(this).atZone(ZoneId.systemDefault()).toLocalDate().toString()
}

operator fun <T> ArrayList<T>.plusAssign(b: List<T>) {
    this.addAll(b)
}

val Float.percent2: String
    get() = DecimalFormat("##.##%").format(this)

val Float.percent1: String
    get() = DecimalFormat("##.#%").format(this)

val Double.percent2: String
    get() = DecimalFormat("##.##%").format(this)

val Double.percent1: String
    get() = DecimalFormat("##.#%").format(this)

val Float.digit2: String
    get() = DecimalFormat(".00").format(this)


/**
 * 判断某activity是否处于栈顶
 * @return  true在栈顶 false不在栈顶
 */
fun isActivityTop(cls: Class<*>, context: Context): Boolean {
    val manager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
    val name = manager.getRunningTasks(1)[0].topActivity!!.className
    return name == cls.name
}

/**
 * Emoji表情校验
 *
 * @param string
 * @return
 */
fun isEmoji(string: String?): Boolean {
    //过滤Emoji表情
    val p: Pattern = Pattern.compile("[^\\u0000-\\uFFFF]")
    //过滤Emoji表情和颜文字
    //Pattern p = Pattern.compile("[\\ud83c\\udc00-\\ud83c\\udfff]|[\\ud83d\\udc00-\\ud83d\\udfff]|[\\u2600-\\u27ff]|[\\ud83e\\udd00-\\ud83e\\uddff]|[\\u2300-\\u23ff]|[\\u2500-\\u25ff]|[\\u2100-\\u21ff]|[\\u0000-\\u00ff]|[\\u2b00-\\u2bff]|[\\u2d06]|[\\u3030]");
    val m: Matcher = p.matcher(string)
    return m.find()
}

val emoji: Regex =
    "(?:[\uD83C\uDF00-\uD83D\uDDFF]|[\uD83E\uDD00-\uD83E\uDDFF]|[\uD83D\uDE00-\uD83D\uDE4F]|[\uD83D\uDE80-\uD83D\uDEFF]|[\u2600-\u26FF]\uFE0F?|[\u2700-\u27BF]\uFE0F?|\u24C2\uFE0F?|[\uD83C\uDDE6-\uD83C\uDDFF]{1,2}|[\uD83C\uDD70\uD83C\uDD71\uD83C\uDD7E\uD83C\uDD7F\uD83C\uDD8E\uD83C\uDD91-\uD83C\uDD9A]\uFE0F?|[\u0023\u002A\u0030-\u0039]\uFE0F?\u20E3|[\u2194-\u2199\u21A9-\u21AA]\uFE0F?|[\u2B05-\u2B07\u2B1B\u2B1C\u2B50\u2B55]\uFE0F?|[\u2934\u2935]\uFE0F?|[\u3030\u303D]\uFE0F?|[\u3297\u3299]\uFE0F?|[\uD83C\uDE01\uD83C\uDE02\uD83C\uDE1A\uD83C\uDE2F\uD83C\uDE32-\uD83C\uDE3A\uD83C\uDE50\uD83C\uDE51]\uFE0F?|[\u203C\u2049]\uFE0F?|[\u25AA\u25AB\u25B6\u25C0\u25FB-\u25FE]\uFE0F?|[\u00A9\u00AE]\uFE0F?|[\u2122\u2139]\uFE0F?|\uD83C\uDC04\uFE0F?|\uD83C\uDCCF\uFE0F?|[\u231A\u231B\u2328\u23CF\u23E9-\u23F3\u23F8-\u23FA]\uFE0F?)".toRegex()

fun today(pattern: String, date: Date = Date()): String =
    SimpleDateFormat(pattern, Locale.getDefault()).format(date)

